package com.ruoze.bdp.function.impl;

import com.ruoze.bdp.constants.Constants;
import com.ruoze.bdp.constants.DataSourceConstants;
import com.ruoze.bdp.entity.datasource.BaseDataSourceParamDTO;
import com.ruoze.bdp.entity.datasource.ClickHouseConnectionParam;
import com.ruoze.bdp.entity.datasource.ClickHouseDataSourceParamDTO;
import com.ruoze.bdp.entity.datasource.MySQLConnectionParam;
import com.ruoze.bdp.enums.DbType;
import com.ruoze.bdp.function.ConnectionParam;
import com.ruoze.bdp.function.DataSourceProcessor;
import com.ruoze.bdp.utils.JSONUtils;

@Deprecated
public class ClickHouseDataSourceProcessor implements DataSourceProcessor {
    @Override
    public DbType getDbType() {
        return DbType.CLICKHOUSE;
    }

    @Override
    public ConnectionParam createConnectionParams(String connectionJson) {
        return JSONUtils.GSON.fromJson(connectionJson, ClickHouseConnectionParam.class);
    }

    @Override
    public ConnectionParam createConnectionParams(BaseDataSourceParamDTO datasourceParam) {
        ClickHouseDataSourceParamDTO clickHouseDataSourceParam = (ClickHouseDataSourceParamDTO) datasourceParam;
        String address = String.format("%s%s:%s", DataSourceConstants.JDBC_CLICKHOUSE, clickHouseDataSourceParam.getHost(), clickHouseDataSourceParam.getPort());
        String jdbcUrl = String.format("%s/%s", address, clickHouseDataSourceParam.getDatabase());

        ClickHouseConnectionParam clickHouseConnectionParam = new ClickHouseConnectionParam();
        clickHouseConnectionParam.setJdbcUrl(jdbcUrl);
        clickHouseConnectionParam.setJdbcUrl(jdbcUrl);
        clickHouseConnectionParam.setDatabase(clickHouseDataSourceParam.getDatabase());
        clickHouseConnectionParam.setAddress(address);
        clickHouseConnectionParam.setUser(clickHouseDataSourceParam.getUserName());
        //TODO 密码加密
        clickHouseConnectionParam.setPassword(clickHouseDataSourceParam.getPassword());
        clickHouseConnectionParam.setDriverClassName(getDatasourceDriver());
        clickHouseConnectionParam.setValidationQuery(getValidationQuery());
        clickHouseConnectionParam.setProps(clickHouseDataSourceParam.getOther());

        return clickHouseConnectionParam;
    }

    @Override
    public BaseDataSourceParamDTO createDatasourceParamDTO(String connectionJson) {
        MySQLConnectionParam connectionParams = (MySQLConnectionParam) createConnectionParams(connectionJson);
        ClickHouseDataSourceParamDTO clickHouseDataSourceParamDTO = new ClickHouseDataSourceParamDTO();

        clickHouseDataSourceParamDTO.setUserName(connectionParams.getUser());
        clickHouseDataSourceParamDTO.setDatabase(connectionParams.getDatabase());

        String address = connectionParams.getAddress();
        String[] hostSeparator = address.split(Constants.DOUBLE_SLASH);
        String[] hostPortArray = hostSeparator[hostSeparator.length - 1].split(Constants.COMMA);
        clickHouseDataSourceParamDTO.setPort(Integer.parseInt(hostPortArray[0].split(Constants.COLON)[1]));
        clickHouseDataSourceParamDTO.setHost(hostPortArray[0].split(Constants.COLON)[0]);

        return clickHouseDataSourceParamDTO;
    }

    @Override
    public BaseDataSourceParamDTO castDatasourceParamDTO(String paramJson) {
        return JSONUtils.GSON.fromJson(paramJson, ClickHouseDataSourceParamDTO.class);
    }

    @Override
    public DataSourceProcessor create() {
        return new ClickHouseDataSourceProcessor();
    }

    @Override
    public String getDatasourceDriver() {
        return DataSourceConstants.COM_CLICKHOUSE_JDBC_DRIVER;
    }

    @Override
    public String getValidationQuery() {
        return DataSourceConstants.CLICKHOUSE_VALIDATION_QUERY;
    }
}
